package com.atung.parking.controller;

import com.atung.parking.config.auth.AuthUserMangerContext;
import com.atung.parking.config.auth.entity.AuthUserEntity;
import com.atung.parking.config.permission.PermissionValid;
import com.atung.parking.config.result.ApiResult;
import com.atung.parking.config.result.PageInfoResponse;
import com.atung.parking.config.result.PageResponse;
import com.atung.parking.entity.BusLicensePlate;
import com.atung.parking.entity.BusUser;
import com.atung.parking.enums.AdminType;
import com.atung.parking.mapstruct.UserConverter;
import com.atung.parking.request.query.QueryUserRequest;
import com.atung.parking.response.page.UserInfoResponse;
import com.atung.parking.response.page.UserResponse;
import com.atung.parking.service.LicensePlateService;
import com.atung.parking.service.UserManagerService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * @author Administrator
 */
@RestController
@RequestMapping("/api/user")
@Api(tags = "用户管理")
public class UserManagerController {
    @Resource
    private UserManagerService userManagerService;
    @Resource
    private UserConverter userConverter;

    @Resource
    private LicensePlateService licensePlateService;
    @GetMapping("/query")
    @ApiOperation("获取用户列表")
    public ApiResult<PageResponse<UserResponse>> queryUserList(QueryUserRequest request) {
        PageInfoResponse<BusUser> userPageInfoResponse = userManagerService.queryUserListService(request);
        List<UserResponse> data = userPageInfoResponse.getData().stream().
                map(item -> {
                    UserResponse item1 = userConverter.toResponse(item);
                    List<BusLicensePlate> busLicensePlate = licensePlateService.queryUseByUserId(item.getId());
                    item1.setLicenseNumbers(busLicensePlate.stream().map(BusLicensePlate::getLicenseNumber).collect(Collectors.toList()));
                    item1.setUserName(item.getWxName());
                    return item1;
                }).collect(Collectors.toList());
        return ApiResult.ok(data, request.getPageNum(), request.getPageSize(), userPageInfoResponse.getTotal());
    }

    @GetMapping("/info")
    @ApiOperation("获取用户信息")
    @PermissionValid(roleType = {AdminType.USER, AdminType.ADMIN, AdminType.SUPERADMIN})
    public ApiResult<UserInfoResponse> queryUserInfo() {

        AuthUserEntity authUserEntity = AuthUserMangerContext.getAuthUserEntity();
        if (Objects.isNull(authUserEntity)) {
            return ApiResult.fail();
        }
        UserInfoResponse userInfoResponse = new UserInfoResponse();
        BeanUtils.copyProperties(authUserEntity, userInfoResponse);
        userInfoResponse.setUserRole(authUserEntity.getRoleType().name());
        return ApiResult.ok(userInfoResponse);
    }
}
